/*
* @Author: songwuhong
* @Date:   2017-04-10 14:57:41
 * @Last Modified by:   dengjiayao
 * @Last Modified time: 2017-08-31 16:30:19
*/
import datepicker from 'vue-datepicker'
import { modal } from 'vue-strap'
import getDatePickerOption from '../../../../util/date-picker-option'
import { showMsg, saveinvestLecture, toogleSectionListFresh } from '../../../../vuex/action'

export default {
  name: 'save',
  components: {
    'vue-modal': modal,
    'vue-datepicker': datepicker
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xToogleIndexAdListFresh: toogleSectionListFresh,
      xSaveinvestLecture: saveinvestLecture
    }
  },
  props: ['showSave', 'it', 'title'],
  data() {
    return {
      datePickerOption: getDatePickerOption()
    }
  },
  methods: {
    submit() {
      var params = {
        title: this.it.title,
        speaker: this.it.speaker,
        lectureUrl: this.it.lectureUrl,
        startTime: this.it.startTime,
        endTime: this.it.endTime
      }

      if (!this.$els.file.files[0] && !this.it.picUrl) {
        return this.xShowMsg({
          type: 'danger',
          content: '* 标记的均为必填/必选项'
        })
      }

      var fields = ['title', 'speaker', 'lectureUrl', 'startTime', 'endTime']
      for (let k of fields) {
        let v = params[k]
        if (v && v.length) {

        } else {
          return this.xShowMsg({
            type: 'danger',
            content: '* 标记的均为必填/必选项'
          })
        }
      }

      if (params.startTime >= params.endTime) {
        return this.xShowMsg({
          type: 'danger',
          content: '开始时间应早于结束时间'
        })
      }
      if (this.it.id) {
        params.id = this.it.id
      }
      if (this.it.contentUrl) {
        params.contentUrl = this.it.contentUrl
      }
      var formData = new FormData()
      for (let name in params) {
        if (name !== 'picFile') {
          formData.append(name, params[name])
        }
      }
      formData.append('picFile', this.$els.file.files[0])

      this.xSaveinvestLecture(formData)
        .then((data) => {
          this.showSave = false
          this.xShowMsg({
            type: 'success',
            content: '操作成功！'
          })
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    showSave() {
      if (!this.showSave) {
        this.xToogleIndexAdListFresh()
      }
    }
  },
}
